package c15;

public class DeadLockDemo {
    static Object objectA = new Object();
    static Object objectB = new Object();


    private static void startThreadA() {
        Thread t = new Thread() {
            public void run() {
              synchronized (objectA) {
                  System.out.println(Thread.currentThread().getName()+"持有锁A");
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  synchronized (objectB) {
                      System.out.println(Thread.currentThread().getName()+"持有锁B");
                  }
              }
            }
        };
        t.start();
    }

    private static void startObjectB() {
        Thread t = new Thread() {
            public void run() {
                synchronized (objectB) {
                    System.out.println(Thread.currentThread().getName()+"持有锁B");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (objectA) {
                        System.out.println(Thread.currentThread().getName()+"持有锁A");
                    }
                }
            }
        };
        t.start();
    }

    public static void main(String[] args) {
        startThreadA();
        startObjectB();
        System.out.println("end");
    }
}
